package br.com.cfccultura.admin.control.actions.cadastros;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Component;

import br.com.cfccultura.admin.control.actions.DefaultAction;
import br.com.cfccultura.admin.control.forms.cadastros.CadastroSalaAulaForm;
import br.com.cfccultura.admin.model.enums.Ativo;
import br.com.cfccultura.admin.model.vo.Sala;

@Component("/cadastroSalaAula")
public class CadastroSalaAulaAction extends DefaultAction {
	
	private static final Logger logger = Logger.getLogger(CadastroSalaAulaAction.class); 
	
	public ActionForward iniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroSalaAulaForm salaForm = (CadastroSalaAulaForm) form;
		consultar(salaForm, request);
		
		return mapping.findForward("body");
	}
	
	public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroSalaAulaForm salaForm = (CadastroSalaAulaForm) form;
		consultar(salaForm, request);
		
		return mapping.findForward("body");
	}
	
	public ActionForward incluir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroSalaAulaForm salaForm = (CadastroSalaAulaForm) form;
		Sala vo = new Sala();
		vo.setDescricao(salaForm.getDescricao());
		vo.setQtdeAluno(new Integer(salaForm.getQtdeAluno()));
		getSalaAulaDAO().incluir(vo);
		
		return null;
	}	
	
	public ActionForward alterarIniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroSalaAulaForm salaForm = (CadastroSalaAulaForm) form;
		List<Sala> lista = getSalaAulaDAO().consultar(new Sala(salaForm.getIdSelecionado()));
		 
		Sala vo = lista.get(0);
		printJsonObjectToStream(vo, response);
		
		return null;
	}	
	
	public ActionForward alterar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroSalaAulaForm salaForm = (CadastroSalaAulaForm) form;
		Sala vo = new Sala();
		vo.setCodigo(salaForm.getIdSelecionado());
		vo.setDescricao(salaForm.getDescricao());
		vo.setQtdeAluno(new Integer(salaForm.getQtdeAluno()));
		getSalaAulaDAO().alterar(vo);
		
		return null;
	}
	
	public ActionForward excluir(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
		
		CadastroSalaAulaForm salaForm = (CadastroSalaAulaForm) form;
		getSalaAulaDAO().excluir(salaForm.getIdSelecionado());
		
		return null;
	}

	
	private void consultar(CadastroSalaAulaForm salaForm, HttpServletRequest request) {
		Sala vo = new Sala();
		vo.setAtivo(Ativo.setValue(salaForm.getAtivoConsultar()));
		
		List<Sala> listaResultado = getSalaAulaDAO().consultar(vo);
		request.setAttribute("listaResultado", listaResultado);
	}
}
